package com.shankungfu.main;

import java.util.HashMap;

import cn.sharesdk.douban.Douban;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.utils.UIHandler;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.sina.weibo.SinaWeibo.ShareParams;
import cn.sharesdk.system.email.Email;
import cn.sharesdk.system.text.ShortMessage;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.tencent.weibo.TencentWeibo;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

import com.shankungfu.R;
import com.shankungfu.util.TestUtils;
import com.shankungfu.util.ToastUtil;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;

public class WeiboMoreActivity extends Activity implements OnClickListener {

	protected static final int ERROR = 0x10;
	protected static final int SUCCESS = 0x11;
	protected static final int CANCEL = 0x12;
	private static final int MSG_TOAST = 1;
	private static final int MSG_ACTION_CCALLBACK = 2;
	private static final int MSG_CANCEL_NOTIFY = 3;
	private Context context;
	private String title;
	private String content;
	private String file_path;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);

			int kind = (Integer) msg.obj;

			switch (msg.what) {
			case ERROR:
				if (Platform.ACTION_AUTHORIZING == kind) {
					ToastUtil.showMessageText(context, "授权错误");
				} else if (Platform.ACTION_SHARE == kind) {
					ToastUtil.showMessageText(context, "发布失败");
				}
				break;
			case SUCCESS:
				if (Platform.ACTION_AUTHORIZING == kind) {
					ToastUtil.showMessageText(context, "授权成功");
				} else if (Platform.ACTION_SHARE == kind) {
					ToastUtil.showMessageText(context, "发布成功");
				}
				break;
			case CANCEL:
				if (Platform.ACTION_AUTHORIZING == kind) {
					ToastUtil.showMessageText(context, "取消授权");
				} else if (Platform.ACTION_SHARE == kind) {
					ToastUtil.showMessageText(context, "取消");
				}
				break;

			default:
				break;
			}

		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.all_weibo_layout);
		context = WeiboMoreActivity.this;

		Intent intent = getIntent();
		title = intent.getStringExtra(PickItemDetailActivity.SHARE_TITLE);
		content = intent.getStringExtra(PickItemDetailActivity.SHARE_CONTENT);
		file_path = intent
				.getStringExtra(PickItemDetailActivity.SHARE_FILE_PATH);

	}

	@Override
	public void onClick(View v) {

		ShareSDK.initSDK(this);

		switch (v.getId()) {
		case R.id.cancel:
			finish();
			break;
		case R.id.weibo_sinaweibo_btn:
			sinaWeibo();
			break;
		case R.id.weibo_wechatmoments_btn:
			wechatmoments();
			break;
		case R.id.weibo_wechat_btn:
			wechat();
			break;
		case R.id.weibo_qzone_btn:
			qzone();
			break;
		case R.id.weibo_douban_btn:
			douban();
			break;
		case R.id.weibo_shortmessage_btn:
			shortmessage();
			break;
		case R.id.weibo_email_btn:
			email();
			break;

		}

	}

	private void email() {
		Platform emailPlatform = ShareSDK.getPlatform(context, Email.NAME);
		cn.sharesdk.system.email.Email.ShareParams sp = new cn.sharesdk.system.email.Email.ShareParams();
		
		sp.text = content;
		sp.imagePath = file_path;
		sp.title = title;
		sp.address = "741227905@qq.com";
		
		emailPlatform.setPlatformActionListener(new PlatformActionListener() {

			@Override
			public void onError(Platform arg0, int arg1, Throwable arg2) {

				Message msg = new Message();
				msg.what = ERROR;
				msg.obj = arg1;
				handler.sendMessage(msg);

				TestUtils.logE("发送 " + arg0.getName() + " 失败！原因是：" + arg2);
			}

			@Override
			public void onComplete(Platform arg0, int arg1,
					HashMap<String, Object> arg2) {

				Message msg = new Message();
				msg.what = SUCCESS;
				msg.obj = arg1;
				handler.sendMessage(msg);

				TestUtils.logE("发送 " + arg0.getName() + " 成功");

			}

			@Override
			public void onCancel(Platform arg0, int arg1) {
				Message msg = new Message();
				msg.what = CANCEL;
				msg.obj = arg1;
				handler.sendMessage(msg);
				TestUtils.logE("发送 " + arg0.getName() + " 取消");
			}
		});
		if (emailPlatform.isValid()) {
			emailPlatform.share(sp);
		} else {
			emailPlatform.authorize();
		}
	}

	private void shortmessage() {
		Platform shortmessagePlatform = ShareSDK.getPlatform(context,
				ShortMessage.NAME);
		cn.sharesdk.system.text.ShortMessage.ShareParams sp = new cn.sharesdk.system.text.ShortMessage.ShareParams();
		
		sp.text = content;
		sp.imagePath = file_path;
		sp.title = title;
		sp.address = "15301129568";
		
		shortmessagePlatform
				.setPlatformActionListener(new PlatformActionListener() {

					@Override
					public void onError(Platform arg0, int arg1, Throwable arg2) {

						Message msg = new Message();
						msg.what = ERROR;
						msg.obj = arg1;
						handler.sendMessage(msg);

						TestUtils.logE("发送 " + arg0.getName() + " 失败！原因是："
								+ arg2);
					}

					@Override
					public void onComplete(Platform arg0, int arg1,
							HashMap<String, Object> arg2) {

						Message msg = new Message();
						msg.what = SUCCESS;
						msg.obj = arg1;
						handler.sendMessage(msg);

						TestUtils.logE("发送 " + arg0.getName() + " 成功");

					}

					@Override
					public void onCancel(Platform arg0, int arg1) {
						Message msg = new Message();
						msg.what = CANCEL;
						msg.obj = arg1;
						handler.sendMessage(msg);
						TestUtils.logE("发送 " + arg0.getName() + " 取消");
					}
				});
		if (shortmessagePlatform.isValid()) {
			shortmessagePlatform.share(sp);
		} else {
			shortmessagePlatform.authorize();
		}
	}

	private void qzone() {
		Platform qzonePlatform = ShareSDK.getPlatform(context, QZone.NAME);
		cn.sharesdk.tencent.qzone.QZone.ShareParams sp = new cn.sharesdk.tencent.qzone.QZone.ShareParams();
		
		sp.text = content;
		sp.imagePath = file_path;
		sp.title = title;
		
		qzonePlatform.setPlatformActionListener(new PlatformActionListener() {

			@Override
			public void onError(Platform arg0, int arg1, Throwable arg2) {

				Message msg = new Message();
				msg.what = ERROR;
				msg.obj = arg1;
				handler.sendMessage(msg);

				TestUtils.logE("发送 " + arg0.getName() + " 失败！原因是：" + arg2);
			}

			@Override
			public void onComplete(Platform arg0, int arg1,
					HashMap<String, Object> arg2) {

				Message msg = new Message();
				msg.what = SUCCESS;
				msg.obj = arg1;
				handler.sendMessage(msg);

				TestUtils.logE("发送 " + arg0.getName() + " 成功");

			}

			@Override
			public void onCancel(Platform arg0, int arg1) {
				Message msg = new Message();
				msg.what = CANCEL;
				msg.obj = arg1;
				handler.sendMessage(msg);
				TestUtils.logE("发送 " + arg0.getName() + " 取消");
			}
		});
		if (qzonePlatform.isValid()) {
			qzonePlatform.share(sp);
		} else {
			qzonePlatform.authorize();
		}
	}

	private void wechat() {
		Platform wechatPlatform = ShareSDK.getPlatform(context, Wechat.NAME);
		cn.sharesdk.wechat.friends.Wechat.ShareParams sp = new cn.sharesdk.wechat.friends.Wechat.ShareParams();
		
		sp.text = content;
		sp.imagePath = file_path;
		sp.title = title;
		sp.shareType = Platform.SHARE_WEBPAGE;

		wechatPlatform.setPlatformActionListener(new PlatformActionListener() {

			@Override
			public void onError(Platform arg0, int arg1, Throwable arg2) {

				Message msg = new Message();
				msg.what = ERROR;
				msg.obj = arg1;
				handler.sendMessage(msg);

				TestUtils.logE("发送 " + arg0.getName() + " 失败！原因是：" + arg2);
			}

			@Override
			public void onComplete(Platform arg0, int arg1,
					HashMap<String, Object> arg2) {

				Message msg = new Message();
				msg.what = SUCCESS;
				msg.obj = arg1;
				handler.sendMessage(msg);

				TestUtils.logE("发送 " + arg0.getName() + " 成功");

			}

			@Override
			public void onCancel(Platform arg0, int arg1) {
				Message msg = new Message();
				msg.what = CANCEL;
				msg.obj = arg1;
				handler.sendMessage(msg);
				TestUtils.logE("发送 " + arg0.getName() + " 取消");
			}
		});
		if (wechatPlatform.isValid()) {
			wechatPlatform.share(sp);
		} else {
			wechatPlatform.authorize();
		}
	}

	private void wechatmoments() {
		Platform wechatmomentsPlatform = ShareSDK.getPlatform(context,
				WechatMoments.NAME);
		cn.sharesdk.wechat.moments.WechatMoments.ShareParams sp = new cn.sharesdk.wechat.moments.WechatMoments.ShareParams();

		sp.text = content;
		sp.imagePath = file_path;
		sp.title = title;
		sp.shareType = Platform.SHARE_WEBPAGE;

		wechatmomentsPlatform
				.setPlatformActionListener(new PlatformActionListener() {

					@Override
					public void onError(Platform arg0, int arg1, Throwable arg2) {

						Message msg = new Message();
						msg.what = ERROR;
						msg.obj = arg1;
						handler.sendMessage(msg);

						TestUtils.logE("发送 " + arg0.getName() + " 失败！原因是："
								+ arg2);
					}

					@Override
					public void onComplete(Platform arg0, int arg1,
							HashMap<String, Object> arg2) {

						Message msg = new Message();
						msg.what = SUCCESS;
						msg.obj = arg1;
						handler.sendMessage(msg);

						TestUtils.logE("发送 " + arg0.getName() + " 成功");

					}

					@Override
					public void onCancel(Platform arg0, int arg1) {
						Message msg = new Message();
						msg.what = CANCEL;
						msg.obj = arg1;
						handler.sendMessage(msg);
						TestUtils.logE("发送 " + arg0.getName() + " 取消");
					}
				});
		if (wechatmomentsPlatform.isValid()) {
			wechatmomentsPlatform.share(sp);
		} else {
			wechatmomentsPlatform.authorize();
		}
	}

	private void douban() {
		Platform doubanPlatform = ShareSDK.getPlatform(context, Douban.NAME);
		cn.sharesdk.douban.Douban.ShareParams sp = new cn.sharesdk.douban.Douban.ShareParams();
		
		sp.text = content;
		sp.imagePath = file_path;

		doubanPlatform.setPlatformActionListener(new PlatformActionListener() {

			@Override
			public void onError(Platform arg0, int arg1, Throwable arg2) {

				Message msg = new Message();
				msg.what = ERROR;
				msg.obj = arg1;
				handler.sendMessage(msg);

				TestUtils.logE("发送 " + arg0.getName() + " 失败！原因是：" + arg2);
			}

			@Override
			public void onComplete(Platform arg0, int arg1,
					HashMap<String, Object> arg2) {

				Message msg = new Message();
				msg.what = SUCCESS;
				msg.obj = arg1;
				handler.sendMessage(msg);

				TestUtils.logE("发送 " + arg0.getName() + " 成功");

			}

			@Override
			public void onCancel(Platform arg0, int arg1) {
				Message msg = new Message();
				msg.what = CANCEL;
				msg.obj = arg1;
				handler.sendMessage(msg);
				TestUtils.logE("发送 " + arg0.getName() + " 取消");
			}
		});
		if (doubanPlatform.isValid()) {
			doubanPlatform.share(sp);
		} else {
			doubanPlatform.authorize();
		}

	}

	private void sinaWeibo() {
		Platform sinaPlatform = ShareSDK.getPlatform(context, SinaWeibo.NAME);
		ShareParams sp = new ShareParams();
		sp.text = content;
		sp.imagePath = file_path;

		sinaPlatform.setPlatformActionListener(new PlatformActionListener() {

			@Override
			public void onError(Platform arg0, int arg1, Throwable arg2) {

				Message msg = new Message();
				msg.what = ERROR;
				msg.obj = arg1;
				handler.sendMessage(msg);

				TestUtils.logE("发送 " + arg0.getName() + " 失败！原因是：" + arg2);
			}

			@Override
			public void onComplete(Platform arg0, int arg1,
					HashMap<String, Object> arg2) {

				Message msg = new Message();
				msg.what = SUCCESS;
				msg.obj = arg1;
				handler.sendMessage(msg);

				TestUtils.logE("发送 " + arg0.getName() + " 成功");

			}

			@Override
			public void onCancel(Platform arg0, int arg1) {
				Message msg = new Message();
				msg.what = CANCEL;
				msg.obj = arg1;
				handler.sendMessage(msg);
				TestUtils.logE("发送 " + arg0.getName() + " 取消");
			}
		});
		if (sinaPlatform.isValid()) {
			sinaPlatform.share(sp);
		} else {
			sinaPlatform.authorize();
		}
	}
	
	// 在状态栏提示分享操作
		private void showNotification(long cancelTime, String text) {
			try {
				Context app = context.getApplicationContext();
				NotificationManager nm = (NotificationManager) app
						.getSystemService(Context.NOTIFICATION_SERVICE);
				final int id = Integer.MAX_VALUE / 13 + 1;
				nm.cancel(id);

				long when = System.currentTimeMillis();
				Notification notification = new Notification(R.drawable.ic_launcher, text, when);
				PendingIntent pi = PendingIntent.getActivity(app, 0, new Intent(), 0);
				notification.setLatestEventInfo(app, "分享操作", text, pi);
				notification.flags = Notification.FLAG_AUTO_CANCEL;
				nm.notify(id, notification);

				if (cancelTime > 0) {
					Message msg = new Message();
					msg.what = MSG_CANCEL_NOTIFY;
					msg.obj = nm;
					msg.arg1 = id;
					UIHandler.sendMessageDelayed(msg, cancelTime, null);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

}
